<template>
	<view class="login_box">
		<view class="text_login">
			<!-- logo -->
			<image class="login_logo"  src="../../static/logo/orange.png" alt="" />
			
			<!-- 表单区域 -->
			<view class="login_phone">
				<text>手机号</text>
				<input type="text"  maxlength="11"  v-model.number="phone"/>
			</view>
			<view class="login_code">
				<text>验证码</text>
				<input type="text"  maxlength="4" v-model="code"/>
				<view hover-class="touchOpacity" @click="getsms">{{codeText}}</view>
			</view>
			
			<!-- 登录按钮 -->
			<view class="login_but" hover-class="touchOpacity" @click="login" >
				<text space="ensp" >登    录</text> 
			</view>
		</view>
	</view>
</template>

<script>
	import {getSms,smslogin} from'@/api/api'
	export default {
		data() {
			return {
				phone:"",
				codeFalg:false,
				codeText:'获取验证码',
				code:""
			}
		},
		onLoad() {
		},
		methods: {
			
			login(){
				let phone =  this.phone
				let code = this.code 
				let sPhone = uni.getStorageSync('phone') || null
				let scode =   uni.getStorageSync('code') || null
				//验证手机号
				
				if(phone!=sPhone ||  sPhone===null||phone===''){
					uni.showToast({
						title:"手机号格式不正确"
					})
		       	return false 
				}
		
			
				if(code!=scode ||  scode===null||code===''){
					uni.showToast({
						title:"验证码错误",
						icon:'none'
					})
						return false 	
				}
			smslogin({phone:phone}).then(res=>{
				
			
					if(res.data.list.code===200){
						uni.showToast({
							icon:screen,
							title:'登录成功',
							duration:1000,
					
						
						})
					}
					uni.removeStorageSync("phone")
					uni.removeStorageSync("code")	
					this.$store.commit('RECORD_USERINFO',res.data.list)
					uni.setStorageSync('userinof',res.data.list)
						uni.showToast({
							title:'登录成功'
						})	
						uni.switchTab({
							url:"/pages/home/index"
						})
						
							
				

			
			
			})	
				
				
			},
			//  获取验证码
			getsms(){		
				
				if(this.codeFalg){return false}
					this.codeFalg = true
				let phone =  this.phone
				getSms({phone:this.phone}).then(res=>{
					if(res.data.code===200){
						this.code = res.data.list.code
					uni.setStorageSync("code",res.data.list.code)
					uni.setStorageSync("phone",phone)
						let num  = 10
						let timer = setInterval(()=>{
							num--
							this.codeText = num
							if(num<1){
							this.codeText = true 	
							clearInterval(timer)
							this.num = 10 
							this.codeText = '获取验证码'
						this.codeFalg = false	
							}
						},1000)
						
				
					}else{
						uni.showToast({
							title:'请填写手机号',
							success:(res=>{
										this.codeFalg = false
							}) 
						})
			
					}
					
					
					
				})
				
			},
	
			
		}
}
</script>

<style>
	/* 引入样式 */
	@import url("@/static/css/textlogin.css");
</style>
